package Client;
import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

public class Sign_in extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;

	/**
	 * Launch the application.
	 */
	static Sign_in frame;
	static int client_id;
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					frame = new Sign_in();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Sign_in() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 330, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblSignIn = new JLabel("Sign In");
		lblSignIn.setFont(new Font("Tahoma", Font.PLAIN, 18));
		lblSignIn.setBounds(107, 30, 80, 29);
		contentPane.add(lblSignIn);
		
		JLabel lblUserId = new JLabel("User ID");
		lblUserId.setBounds(53, 92, 69, 14);
		contentPane.add(lblUserId);
		
		JLabel lblPassword = new JLabel("Password");
		lblPassword.setBounds(53, 146, 69, 14);
		contentPane.add(lblPassword);
		
		textField = new JTextField();
		textField.setBounds(158, 89, 86, 20);
		contentPane.add(textField);
		textField.setColumns(10);
		
		textField_1 = new JTextField();
		textField_1.setColumns(10);
		textField_1.setBounds(158, 143, 86, 20);
		contentPane.add(textField_1);
		
		
		JButton btnNewButton = new JButton("Sign In");
		
		
		
		
		
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
								
				client_id = Integer.parseInt(textField.getText());
				System.out.println(client_id);
				String pass = textField_1.getText();
				System.out.println(pass);
				
				int port=15011;
				String ip=new String("192.168.5.173");		//sever IP
				InetAddress ipad = null;
				
				try{
					ipad = InetAddress.getByName(ip);
					Socket auth = new Socket(ipad,port);
				
					BufferedReader inc  = new BufferedReader (new InputStreamReader(auth.getInputStream()));
				
					PrintWriter outc = new PrintWriter(new OutputStreamWriter(auth.getOutputStream()));
				
				///////////////////////////////////	
					BufferedReader inputStream = new BufferedReader(new FileReader("./src/list_client.txt"));
					int no_net=Integer.parseInt(inputStream.readLine());
					
					int [] gr_ids=new int[no_net];
					for(int i=0;i<no_net;i++)
					{
						gr_ids[i]=Integer.parseInt(inputStream.readLine());
					
						//System.out.println (gr_ids[i]);
					}
					
					create_non_existing_gr_folders(gr_ids,no_net);
					
					//////////////////////////////////////////////////////
					
					
					outc.println(client_id+"@"+pass);
					outc.flush();
					System.out.println("client id sent");
					
				//	outc.write(pass);
					int status = Integer.parseInt(inc.readLine());	//1 for success and 0 for failure
					
						 String temp;
						 BufferedWriter sync_ip = new BufferedWriter(new FileWriter("./src/list_sync_ip.txt"));
				//		 PrintWriter sync_ip_p =new PrintWriter(new FileWriter("list_sync_ip_new.txt"));
						 if(status==1){
						  while(true)
						 {	temp = inc.readLine();
							 System.out.println(temp);
								sync_ip.write(temp);
							 	sync_ip.newLine();
							if(temp.compareTo("a")==0)
								 break;
						 }
						 
						 sync_ip.close();
					//	 Receiver.write();						 
						Success s = new Success();
						s.setVisible(true);
						frame.setVisible(false);
						 }
						 else
						 {
						 System.out.println("LogIn Failed");
						 System.exit(0);
						 }
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					System.out.println("Error in writing File.");
				}
				
			}
		});
		btnNewButton.setBounds(182, 204, 89, 23);
		contentPane.add(btnNewButton);
		
		JButton btnBack = new JButton("Exit");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				System.exit(0);
				
			}
		});
		btnBack.setBounds(53, 204, 89, 23);
		contentPane.add(btnBack);
	}
	public JTextField getTextField() {
		return textField;
	}
	public JTextField getTextField_1() {
		return textField_1;
	}



private static void create_non_existing_gr_folders(int[] gr_ids, int no_net) 
{
	try
	{
		for (int i=0;i<no_net;i++)
		{
	    String dirname =String.valueOf(gr_ids[i]);
	    boolean success = (new File(dirname)).mkdir();
	    if (success) 
	    {
	        System.out.println("Directory: " + dirname + " created");
	    }   
		}
	 }
	catch (Exception e)
	 {
	      System.err.println("Error: " + e.getMessage());
	 }
	
}





private static void list_the_folder(int[] gr_ids, int no_net) 
{
	 String path = "D:\\IIITD\\4th Sem\\CN\\Project\\Project\\";
	  for (int k=0;k<no_net;k++)
	  {
		  String path1=path;
		  System.out.println("---------------------");
		  System.out.println(gr_ids[k]);
		  path1=path1+String.valueOf(gr_ids[k]);
		  String files;
		  File folder = new File(path1);
		  File[] listOfFiles = folder.listFiles(); 
		  for (int i = 0; i < listOfFiles.length; i++) 
		  {
			  if (listOfFiles[i].isFile()) 
			  {
				  files = listOfFiles[i].getName();
				  System.out.println(files);
			  }
		  }
	  }
}










}
